package com.hlt.controller.admin;

import com.hlt.constant.JwtClaimsConstant;
import com.hlt.dto.AdminDTO;
import com.hlt.dto.LoginDTO;
import com.hlt.dto.AdminPageQueryDTO;
import com.hlt.entity.Users;
import com.hlt.properties.JwtProperties;
import com.hlt.result.PageResult;
import com.hlt.result.Result;
import com.hlt.service.AdminService;
import com.hlt.utils.JwtUtil;
import com.hlt.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags = "用户接口")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 管理员登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("管理员登录：{}", loginDTO);

        Users admin = adminService.login(loginDTO);

        // 登录成功后，生成一个token，返回给前端
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put(JwtClaimsConstant.ADMIN_ID,admin.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                tokenMap);
        log.info("登录成功，生成的token为：{}", token);

        LoginVO loginVO = LoginVO.builder()
               .id(Long.valueOf(admin.getUserId()))
                .userName(admin.getUsername())
               .token(token)
               .build();

        return Result.success(loginVO);
    }

    /**
     * 管理员注册
     */
    @PostMapping("/register")
    @ApiOperation("注册")
    public Result<String> register(@RequestBody LoginDTO loginDTO) {
        log.info("注册：{}", loginDTO);
        adminService.register(loginDTO);
        return Result.success();
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping("/save")
    @ApiOperation("新增管理员")
    public Result save(@RequestBody AdminDTO adminDTO){
        log.info("新增管理员：{}",adminDTO);
        adminService.save(adminDTO);
        return Result.success();
    }

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(@RequestBody AdminPageQueryDTO adminPageQueryDTO){
        log.info("分页查询：{}",adminPageQueryDTO);
        PageResult pageResult = adminService.page(adminPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询
     * @param userId
     * @return
     */
    @GetMapping("{userId}")
    @ApiOperation("根据id查询")
    public Result<Users> getById(@PathVariable Long userId){
        log.info("根据id查询：{}",userId);
        Users users = adminService.getById(userId);
        return Result.success(users);
    }

    /**
     * 修改
     * @param adminDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改")
    public Result update(@RequestBody AdminDTO adminDTO){
        log.info("修改：{}",adminDTO);
        adminService.update(adminDTO);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param userid
     * @return
     */
    @DeleteMapping()
    @ApiOperation("根据id删除")
    public Result delete(Integer userid){
        log.info("根据id删除：{}",userid);
        Users users = adminService.getById(userid);
        adminService.removeById(users);
        return Result.success();
    }
}
